package com.sprocomm.agingtest;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.transition.Visibility;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

/**
 * Created by resonlei on 2017/6/20.
 */

public class BaseActivity extends AppCompatActivity {
    public String TAG;
    private WindowManager windowManager;
    private Button btnPass, btnFail;
    private int btnWight;
    private AlertDialog mAlertDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        windowManager = getWindowManager();
        btnWight = Tools.getWinWigth(windowManager) / 2;
        addResultBtn();
    }

    @Override
    protected void onDestroy() {
        Tools.logE("onDestroyBase");
        if (btnPass != null){
            Tools.logE("onDestroy+btnPass");
            getWindowManager().removeViewImmediate(btnPass);//必须用这个方法，不能用removeView。
        }

        if (btnFail != null){
            Tools.logE("onDestroy+btnFail");
            getWindowManager().removeViewImmediate(btnFail);
        }
        if (mAlertDialog != null){
            mAlertDialog.dismiss();
            mAlertDialog = null;
        }
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        Tools.logE("onPause");
        super.onPause();

    }

    @Override
    protected void onStop() {
        Tools.logE("onStop");
        super.onStop();
    }

    /**
     * 添加item测试的pass跟fail按钮
     */
    public void addResultBtn() {
        btnPass = new Button(getApplicationContext());
        btnPass.setText("PASS");
        btnPass.setGravity(Gravity.CENTER);
        btnPass.setEnabled(false);
        btnPass.setVisibility(View.VISIBLE);
        Drawable drawable = getDrawable(R.mipmap.pass);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        btnPass.setCompoundDrawables(drawable, null, null, null);

        btnPass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
                Tools.exitResultNotic(BaseActivity.this, true);
            }
        });

        btnFail = new Button(getApplicationContext());
        btnFail.setText("FAIL");
        btnFail.setGravity(Gravity.CENTER);
        btnFail.setVisibility(View.VISIBLE);
        Drawable drawable1 = getDrawable(R.mipmap.fail);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        btnFail.setCompoundDrawables(drawable1, null, null, null);
        btnFail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
                Tools.exitResultNotic(BaseActivity.this, false, "Determine fail of test?");
            }
        });

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSLUCENT);
        lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        lp.width = btnWight;
        windowManager.addView(btnPass, lp);

        lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
        windowManager.addView(btnFail, lp);
    }

    public void setPassBtnEnable(boolean enable) {
        btnPass.setEnabled(enable);
    }

    public void setPassBtnText(String text) {
        btnPass.setText(text);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            mAlertDialog = Tools.exitNotic(this);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_HOME
                || keyCode == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return true;
    }

    public void setBtnsVisibility(int visibility){
        if (visibility != View.INVISIBLE && visibility != View.VISIBLE && visibility != View.GONE){
            Log.e(TAG,"非法参数");
            return;
        }
        if (btnPass != null){
            btnPass.setVisibility(visibility);
        }
        if (btnFail != null){
            btnFail.setVisibility(visibility);
        }
    }

}
